//
//  SecondRootViewController.m
//  结伴
//
//  Created by eliot on 16/8/8.
//  Copyright © 2016年 aleven. All rights reserved.
//

#import "SecondRootViewController.h"

#import "SDCycleScrollView.h"
#import "MyCollectionViewCell.h"
#import "JPWaterflowLayout.h"
#import "SecondRootCollectionReusableView.h"
#import "DestinationSearchViewController.h"

#import "XqobjViewController.h"
#import "ModeViewController.h"
#import "MoodViewController.h"

@interface SecondRootViewController ()<UISearchBarDelegate,UICollectionViewDelegate,SDCycleScrollViewDelegate, UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout, UISearchBarDelegate>

@property (nonatomic,strong)UISearchBar *searchBar;


@property (nonatomic,strong) NSTimer *time;

@property (strong, nonatomic) UIPageControl *pageControt;


@property (nonatomic, strong) UICollectionView *myCollectView;



@end

@implementation SecondRootViewController




-(UICollectionView *)myCollectView {
    if (!_myCollectView) {
        
<<<<<<< HEAD
//        UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
//        layout.minimumLineSpacing = 0;
//        [layout setScrollDirection:UICollectionViewScrollDirectionVertical];
//        layout.minimumInteritemSpacing = 10;
//        layout.itemSize = CGSizeMake((0.5 * Swidth - 10), 100);
//        layout.sectionInset = UIEdgeInsetsMake(10,0,0, 0);
//        layout.scrollDirection = UICollectionViewScrollDirectionVertical;
=======
        UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
        
        layout.minimumLineSpacing = 0;
        
        [layout setScrollDirection:UICollectionViewScrollDirectionVertical];
        
        layout.minimumInteritemSpacing = 10;
        
        layout.itemSize = CGSizeMake((0.5 * Swidth - 10), 100);
        
        layout.sectionInset = UIEdgeInsetsMake(10,0,0, 0); // (self.view_main.frame.origin.y +self.view_main.frame.size.height + 10)
        layout.scrollDirection = UICollectionViewScrollDirectionVertical;
        
>>>>>>> c7d7fdffde90e058b25ba56bfff0e2c0b07ff511
        JPWaterflowLayout *ll = [[JPWaterflowLayout alloc] init];
        
        ll.headerReferenceSize = CGSizeMake(Swidth, 400);
        _myCollectView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height) collectionViewLayout:ll];
 //       [layout setHeaderReferenceSize:CGSizeMake(_myCollectView.frame.size.width,400)];
        [_myCollectView registerNib:[UINib nibWithNibName:@"MyCollectionViewCell" bundle:nil] forCellWithReuseIdentifier:@"cell"];
        [self.view addSubview:_myCollectView];
        _myCollectView.delegate = self;
          _myCollectView.dataSource = self;
        _myCollectView.backgroundColor = [UIColor whiteColor];
        // 注册头部视图
        
        [_myCollectView registerNib:[UINib nibWithNibName:@"SecondRootCollectionReusableView" bundle:nil] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"SecondRootCollectionReusableView"];
//        [_myCollectView registerClass:[SecondRootCollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"SecondRootCollectionReusableView"];
    
      
    }
    return _myCollectView;
    
}

-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    return 1;
}

-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    
    return 15;
    
}

-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section {

    return CGSizeMake(Swidth, 400);
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    MyCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
    cell.myImage.image = [UIImage imageNamed:[NSString stringWithFormat:@"%ld", (long)indexPath.item]];
    return cell;
    
}

// 设置自定义头部、尾部视图
- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath {
    
    // UICollectionElementKindSectionHeader：头部视图
    // UICollectionElementKindSectionFooter：尾部视图
    // 如果同时自定义头部、尾部视图，可根据 kind 参数判断当前加载的是头部还是尾部视图，然后进行相应配置。
    
    // 重用机制
    SecondRootCollectionReusableView *headerView = (SecondRootCollectionReusableView *)[collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"SecondRootCollectionReusableView" forIndexPath:indexPath];
  //  headerView.displayLabel.text = @"[自定义头部视图]";
    
  
        //对象
        headerView.objetblock = ^(){
        
            XqobjViewController *obj = [[XqobjViewController alloc]init];
            [self.navigationController pushViewController:obj animated:YES];
        };

          //心情
        headerView.MoodClikblock = ^(){
            MoodViewController *mood = [[MoodViewController alloc]init];
            [self.navigationController pushViewController:mood animated:YES];
        };
  
        
        //方式
        headerView.ModeClikblock = ^(){
            ModeViewController *mode = [[ModeViewController alloc]init];
            [self.navigationController pushViewController:mode animated:YES];
        };

 
    
    
    
    
    return headerView;
}

// 设置是否允许移动
- (BOOL)collectionView:(UICollectionView *)collectionView canMoveItemAtIndexPath:(NSIndexPath *)indexPath {
    return YES;
}


- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    //设置导航条的内容
    _searchBar = [[UISearchBar alloc]initWithFrame:CGRectMake(25, 64, [UIScreen mainScreen].bounds.size.width - 50, 40)];
    _searchBar.delegate = self;
    _searchBar.keyboardType = UIKeyboardAppearanceDefault;
    _searchBar.placeholder = @"请输入目的地";
    _searchBar.delegate = self;
    _searchBar.searchBarStyle = UISearchBarStyleDefault;
    _searchBar.barStyle = UIBarStyleDefault;
    [_searchBar setSearchResultsButtonSelected:YES];
    [_searchBar setClipsToBounds:YES];
    
    self.navigationItem.titleView = _searchBar;
    
   // [self headerView];
    self.myCollectView.hidden = NO;
    
}

#pragma mark - searchBarDelegate
- (BOOL)searchBarShouldBeginEditing:(UISearchBar *)searchBar {
    DestinationSearchViewController *vc = [[DestinationSearchViewController alloc] init];
    [self.navigationController pushViewController:vc animated:YES];
    return NO;
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
